﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Noris.Tools.FrxEditor.Components;
using Noris.Schedule.Support;
using Noris.Tools.FrxEditor.SqlCommands;

namespace Noris.Tools.FrxEditor.Data
{
    /// <summary>
    /// Formulář pro zadání dat ReportData
    /// </summary>
    public partial class ReportData_Form : DForm
    {
        /// <summary>
        /// Konstruktor
        /// </summary>
        public ReportData_Form()
        {
            InitializeComponent();
            this.InitData();
        }
        private void InitData()
        {
            this.Editor.ParserSetting = ParserSqlExpressionSetting.SqlExpressionSetting;
        }
        #region Public property
        /// <summary>
        /// Datový objekt, jehož vlastnosti se zde editují.
        /// Setování vede k zobrazení dat objektu.
        /// </summary>
        public ReportData ReportData
        {
            get { return this._ReportData; }
            set { this._ReportDataSet(value); }
        }
        private ReportData _ReportData;
        #endregion
        #region Vazba na datový objekt
        private void _ReportDataSet(ReportData reportData)
        {
            this._ReportData = reportData;
            this.Editor.EditedText = reportData.DataQuery;
        }
        #endregion
        private void button1_Click(object sender, EventArgs e)
        {
            string sqlText = this.Editor.EditedText;
            IEnumerable<ISqlCommand> commands = SqlCommand.Parse(sqlText);
            SqlSelect select = commands.FirstOrDefault(cmd => cmd.SqlCommand == "SELECT") as SqlSelect;

            select.PartTop.Text = "TOP 3 ";
            sqlText = select.Text;

            Steward.ExecuteInTryCatch(
                delegate 
                {
                    this.DataGrid.DataTable = SimpleDataTable.Load(sqlText);
                }, "Zadaný SQL příkaz obsahuje chybu: %0");
        }
    }
}
